﻿#!/usr/bin/env python
# coding=utf-8

import pymysql
import time
from wxpy import *

def connectdb():
    # 打开数据库连接
    db = pymysql.connect(
		host='localhost',
		port=3306,
		user='root',
		passwd='0026888',
		db='wxpy',
		charset='utf8'
	)
    print('连接上了!')
    return db

def querydb(db):
    # 使用cursor()方法获取操作游标 
    cursor = db.cursor(cursor=pymysql.cursors.DictCursor)

    # SQL 查询语句
    #sql = "SELECT * FROM Student \
    #    WHERE Grade > '%d'" % (80)
    sql = "SELECT * FROM user "
    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchone()
        print(results)
        print(results['nick_name'])
        print(results['remark_name'])
        return results;

    except:
        print ("Error: unable to fecth data")
        
def insertdb(db):
    cursor = db.cursor()
    sql = "INSERT INTO message (msg_id, type,senderPuid,receiverPuid,receiverType,is_at,create_time,receive_time, text) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    cursor.execute(sql,("msgId","type","senderid","receId",1,0,"2019-02-15 23:06:10","2019-02-15 23:06:20","你好abc"))
    db.commit()
    cursor.close()

def closedb(db):
    db.close()

def main():
    db = connectdb()    # 连接MySQL数据库
    #insertdb(db)
    re = querydb(db)
    closedb(db)         # 关闭数据库
    # 进入 Python 命令行、让程序保持运行
    embed()

if __name__ == '__main__':
    main()